//
//  DDHomeBannerPageControlView.m
//  DYD
//
//  Created by 邱明 on 2018/2/23.
//  Copyright © 2018年 Diyidan. All rights reserved.
//

#import "DDHomeBannerPageControlView.h"


static const NSString *DDPageControlItemAttributeBackGroundColor = @"pageControlItemAttributeBackGroundColor";
static const NSString *DDPageControlItemAttributeIsHollow = @"pageControlItemAttributeIsHollow";
static const NSString *DDPageControlItemAttributeHollowWidth = @"pageControlItemAttributeHollowWidth"; //default is 1

@interface DDHomeBannerPageControlItemView : UIView

/** 是不是选中状态 */
@property (nonatomic, assign, getter=isSelected) BOOL selected;
/** 选中状态下的属性 */
@property (nonatomic, copy) NSDictionary *selectedAttributes;
/** 未选中状态下的属性 */
@property (nonatomic, copy) NSDictionary *noSelectedAttributes;

@end

@implementation DDHomeBannerPageControlItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = frame.size.width *0.5;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)setSelectedAttributes:(NSDictionary *)selectedAttributes {
    _selectedAttributes = selectedAttributes;
    [self _refreshUIWithSelectedState:self.isSelected];
}

- (void)setNoSelectedAttributes:(NSDictionary *)noSelectedAttributes {
    _noSelectedAttributes = noSelectedAttributes;
    [self _refreshUIWithSelectedState:self.isSelected];
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    [self _refreshUIWithSelectedState:selected];
}

- (void)_refreshUIWithSelectedState:(BOOL)isSelected {
    if (isSelected) {
        [self _refreshUIWithAttributes:self.selectedAttributes];
    } else {
        [self _refreshUIWithAttributes:self.noSelectedAttributes];
    }
}

- (void)_refreshUIWithAttributes:(NSDictionary *)attributes {
    UIColor *backgroundColor = [attributes objectForKey:DDPageControlItemAttributeBackGroundColor];
    BOOL isHollow = [[attributes objectForKey:DDPageControlItemAttributeIsHollow] boolValue];
    BOOL hollowWidth = [[attributes objectForKey:DDPageControlItemAttributeHollowWidth] floatValue];
    if (isHollow) {
        self.layer.borderColor = backgroundColor.CGColor;
        self.layer.borderWidth = hollowWidth ? hollowWidth : 1;
        self.backgroundColor = [UIColor clearColor];
    } else {
        self.backgroundColor = backgroundColor;
        self.layer.borderWidth = 0;
    }
    
}

@end





@interface DDHomeBannerPageControlView ()

@property (nonatomic, strong) NSMutableArray *itemViewArray;

@end

@implementation DDHomeBannerPageControlView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectedColor = [UIColor whiteColor];
        self.noSelectedColor = [UIColor whiteColor];
        self.hollow = YES;
    }
    return self;
}

- (void)setSelectedColor:(UIColor *)selectedColor {
    _selectedColor = selectedColor;
    [self refreshItemViews];
}

- (void)setNoSelectedColor:(UIColor *)noSelectedColor {
    _noSelectedColor = noSelectedColor;
    [self refreshItemViews];
}

- (void)setHollow:(BOOL)hollow {
    _hollow = hollow;
    [self refreshItemViews];
}

- (void)refreshItemViews {
    for (DDHomeBannerPageControlItemView *itemView in self.itemViewArray) {
        [self _refreshItemView:itemView];
    }
}

- (void)_refreshItemView:(DDHomeBannerPageControlItemView *)itemView {
    itemView.selectedAttributes = @{DDPageControlItemAttributeIsHollow : @NO, DDPageControlItemAttributeBackGroundColor : (self.selectedColor ? self.selectedColor : [UIColor whiteColor])};
    itemView.noSelectedAttributes = @{DDPageControlItemAttributeIsHollow : @YES, DDPageControlItemAttributeBackGroundColor : (self.noSelectedColor ? self.noSelectedColor : [UIColor whiteColor]), DDPageControlItemAttributeHollowWidth : @2};
}

- (void)reloadData {
    [self _clearSubViews]; //先清空视图
    if ([self.dataSource respondsToSelector:@selector(numberOfItemInPageControl:)]) {
        NSInteger count = [self.dataSource numberOfItemInPageControl:self];
        NSInteger currentIndex = [self _fetchCurrentSelectedIndex];
        //计算每一个itemView的frame
        CGFloat itemGap = 8;
        CGFloat itemWidth = 8;
        CGFloat itemHeight = itemWidth;
        CGFloat leftGap = (self.frame.size.width - count *itemWidth - (count - 1) *itemGap) *0.5;
        CGFloat itemY = (self.frame.size.height - itemHeight) *0.5;
        for (int i = 0; i < count; i++) {
            DDHomeBannerPageControlItemView *itemView = [self _fetchItemViewWithIndex:i];
            itemView.frame = CGRectMake(leftGap + (itemGap + itemWidth) *i, itemY, itemWidth, itemHeight);
            [self _refreshItemView:itemView];
            [self addSubview:itemView];
        }
        //选中当前应有的下标
        [self selectAtIndex:currentIndex];
    }
}

- (void)selectAtIndex:(NSInteger)index {
    [self.itemViewArray enumerateObjectsUsingBlock:^(DDHomeBannerPageControlItemView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = idx == index;
    }];
}

- (NSInteger)_fetchCurrentSelectedIndex {
    NSInteger index = 0;
    if ([self.dataSource respondsToSelector:@selector(indexOfCurrentSelectItemInPageControl:)]) {
        index = [self.dataSource indexOfCurrentSelectItemInPageControl:self];
    }
    return index;
}

//获取对应下标的视图（从数组中取出则取出，不在数组中创建新的）
- (DDHomeBannerPageControlItemView *)_fetchItemViewWithIndex:(NSInteger)index {
    DDHomeBannerPageControlItemView *view = nil;
    if (index < self.itemViewArray.count) {
        view = [self.itemViewArray objectAtIndex:index];
        return view;
    }
    view = [self _createItemViewWithItemWidth:8];
    [self.itemViewArray addObject:view];
    return view;
}

- (DDHomeBannerPageControlItemView *)_createItemViewWithItemWidth:(CGFloat)itemWidth {
    DDHomeBannerPageControlItemView *view = [[DDHomeBannerPageControlItemView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, itemWidth)];
    return view;
}

- (void)_clearSubViews {
    for (UIView *itemView in self.itemViewArray) {
        [itemView removeFromSuperview];
    }
}

- (NSMutableArray *)itemViewArray {
    if (!_itemViewArray) {
        _itemViewArray = [NSMutableArray array];
    }
    return _itemViewArray;
}

@end
